package event

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/code"
	"gitee.com/liuxuezhan/ar-platform/base-common/auth"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/event"
	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 新增
func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddLogic) Add(req *types.AddEventReq) (resp *types.Empty, err error) {
	userId := auth.GetTokenUid(l.ctx)
	userId = 150004
	if userId == 0 {
		return nil, code.NotLogin
	}

	u, err := l.svcCtx.SettingService.EventService.AddEvent(l.ctx, &event.AddEventReq{
		MerchantId: 1,             // 商户ID
		BrandType:  req.BrandType, // 厂商类型
		BrandName:  req.BrandName, // 厂商名称
		EventNum:   req.EventNum,  // 事件编码
		ApiUrl:     req.ApiUrl,    // api_url
		ApiToken:   req.ApiToken,  // api_token
		Sort:       req.Sort,      // 排序
		Status:     req.Status,    // 状态,0:关,1:开
		OperatorId: userId,        // 操作人id
	})

	if err != nil {
		logc.Error(l.ctx, "AddEvent err:", err)
		return nil, err
	}

	if u.Code != int64(code.SettingRpcSuccessful.Code()) {
		return nil, code.BusinessFailure
	}
	return
}
